package com.sanpu.kxe.business.trade.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.trade.adapter.RebTotalAdapter;
import com.sanpu.kxe.business.trade.bean.MyRebTotalOutput;
import com.sanpu.kxe.business.trade.contract.RebTotalContract;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.presenter.RebTotalPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.view.ScrollableFragment;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;

/**
 * Description：返佣详情
 * Created by 薛勇军 on 2018/8/8.
 */

public class RebTotalFragment extends ScrollableFragment implements RebTotalContract.View {

    @Bind(R.id.list_view)
    RecyclerView vRecyclerview;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.emptyView)
    View emptyView;

    private RebTotalAdapter mTotalAdapter;

    private RebTotalContract.Presenter mPresenter;

    private int nowPage = 0;

    public static RebTotalFragment getInstance(String id) {
        RebTotalFragment fragment = new RebTotalFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TradeConfig.INTENT_REBLVL, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_reb_total;
    }

    @Override
    protected void initView() {
        super.initView();
        String mRebLvl = (String) getArguments().get(TradeConfig.INTENT_REBLVL);
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mTotalAdapter = new RebTotalAdapter();
        vRecyclerview.setAdapter(mTotalAdapter);
        mTotalAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);

//        final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(mTotalAdapter);
//        vRecyclerview.addItemDecoration(headersDecor);
//        mTotalAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
//            @Override
//            public void onChanged() {
//                headersDecor.invalidateHeaders();
//            }
//        });

        mPresenter = new RebTotalPresenter(this, mRebLvl);
        mPresenter.getMyRebTotalList(nowPage);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.getMyRebTotalList(nowPage);
                mTotalAdapter.setEnableLoadMore(true);
            }
        });
        mTotalAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getMyRebTotalList(nowPage);
            }
        }, vRecyclerview);
        mTotalAdapter.setPreLoadNumber(2);
    }

    @Override
    public void showRebTotalListView(List<MyRebTotalOutput> totalOutputs, boolean isFirst) {
        if (totalOutputs != null && totalOutputs.size()>0) {
            emptyView.setVisibility(View.GONE);
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            if (isFirst) {
                mTotalAdapter.setNewData(totalOutputs);
            } else {
                mTotalAdapter.addData(totalOutputs);
            }
            nowPage++;
        } else if (isFirst){
            emptyView.setVisibility(View.VISIBLE);
            vSwipeRefreshLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mTotalAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mTotalAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mTotalAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    public boolean canScrollVertically(int direction) {
        return vRecyclerview != null && vRecyclerview.canScrollVertically(direction);
    }

    @Override
    public void onFlingOver(int y, long duration) {
        if (vRecyclerview != null) {
            vRecyclerview.smoothScrollBy(y, (int) duration);
        }
    }

    @Override
    public void showErrorText(Throwable e) {
        ((BaseTitleActivity)getActivity()).showErrorText(e);
    }

    @Override
    public void showLoadingDialog(boolean cancelable) {
        ((BaseTitleActivity)getActivity()).showLoadingDialog(cancelable);
    }

    @Override
    public void showLoadingDialog(boolean cancelable, String content) {
        ((BaseTitleActivity)getActivity()).showLoadingDialog(cancelable, content);
    }

    @Override
    public void dismissLoadingDialog() {
        ((BaseTitleActivity)getActivity()).dismissLoadingDialog();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(RebTotalContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
